/*=============================================================================================================
 * MyFragment.java
 *
 * 1.프로젝트 : ToYou - 명함관리 어플
 * 2.패 키 지 : com.trillion.toyou
 * 3.작업환경 : Android
 * 2.작 업 자 : 
 * 3.작업일자 : 2014.12.09
 * 4.내    용 : 내 명함 리스트가 출력되는 프래그먼트 화면
 * 5.수 정 자 : -----
 * 6.수정일자 : -----
 * 7.수정내용 : -----
 * =============================================================================================================*/
package com.trillion.toyou;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MyFragment extends Fragment implements OnItemClickListener, OnItemLongClickListener, OnClickListener{
	View view = null;
	// 수정, 삭제 코드
	final int MENU_MODIFY = 202;
	final int MENU_DElETE = 203;
	
	
	// 플래그먼트 관련
	Fragment frag = null;
	FragmentTransaction trans = null;
	
	// db 관련
	DBHelper db = null;
	
	//리스트 뷰 관련
	ListView lv = null;
	DataAdapter2 adapter = null;
	LayoutInflater mInflater = null;	
	ArrayList<Data> al = null;
	Context context = null;
	
	TextView nname = null;
	TextView company= null;
	TextView grade = null;
	TextView department = null;
	
	TextView address = null;
	TextView telNum = null;
	TextView phoneNum = null;
	TextView email= null;
	
	ImageButton add = null;
	ImageButton bt_back = null; 

	LinearLayout root_myitem = null;
	LinearLayout topTitle = null;
	
	
	// 리스트 뷰 클릭 관련
	int pos = -999;
	boolean flag = true;
	
	int aaa = 2;
	
	// 현재의 대표 명함과 새로 클릭된 대표명함의 동일 여부를 체크하기 위한 변수 
	int nid_check_1 = 0;
	int nid_check_2 = 0;
	
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.view = inflater.inflate(R.layout.my_layout, container, false);
		
		MainActivity.page_check = new MainActivity().BACK_LIST;
		
		this.trans = getFragmentManager().beginTransaction();
		
		this.lv = (ListView)this.view.findViewById(R.id.mylistLayout_lv);
		this.context = getActivity();
		this.list();
		this.adapter = new DataAdapter2(getActivity(), al);
		
		this.topTitle = (LinearLayout)this.view.findViewById(R.id.mylistlayout_top);
		
		
		
		
		
		bt_back = (ImageButton)view.findViewById(R.id.myview_btnback);
		bt_back.setOnClickListener(this);
		
		add = (ImageButton)view.findViewById(R.id.myview_btnadd);
		add.setOnClickListener(this);
		
		lv.setAdapter(this.adapter);
		lv.setOnItemClickListener(this);
		lv.setOnItemLongClickListener(this);
		this.topTitle.setOnClickListener(this);
		
//		myList.setOnClickListener(this);
		
		return view;
	}

	
	@Override
	public void onResume() {
		new NfcOnOffChange(this.topTitle, R.drawable.top_bar_on_nfc_my, R.drawable.top_bar_off_nfc_my);
		super.onResume();
	}
	
	
	public void list()
	{

		this.db = new DBHelper(getActivity(), "toyou", null, 1);

		this.al = this.db.selectMine();
	}
	
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		
		frag = null;
		trans = null;
		
		switch (id) {
		case R.id.myview_btnback:
			
			trans = getFragmentManager().beginTransaction();
			frag = new ListFragment();
			trans.replace(R.id.root_layout, frag);
			trans.addToBackStack(null);
			trans.commitAllowingStateLoss();
			
			break;
			
		case R.id.myview_btnadd:
			trans = getFragmentManager().beginTransaction();
			frag = new InputFragment();
			trans.replace(R.id.root_layout, frag);
			trans.addToBackStack(null);
			trans.commitAllowingStateLoss();
			
			break;
		
			
		case R.id.mylistlayout_top:
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
	            Intent intent = new Intent(Settings.ACTION_NFC_SETTINGS);
	            startActivity(intent);
	         } else {
	            Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
	            startActivity(intent);
	         }
			
			break;
		}
		
		
	}



	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
		this.pos = position;
		Data d = this.al.get(this.pos);
//		Toast.makeText(getActivity(), d.getNid()+"", Toast.LENGTH_SHORT).show();
		Bundle bundle = new Bundle();
		bundle.putInt("id", d.getNid());
						
		if(flag){
			frag = null;
			frag = new MyViewFragment();
			frag.setArguments(bundle);
			
			FragmentTransaction trans = getFragmentManager().beginTransaction();
			trans.replace(R.id.root_layout, frag);
			trans.addToBackStack(null);
			trans.commitAllowingStateLoss();
		}
		this.flag = true;
		
	}
	
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		
		flag = false;
		
		this.pos = position;
		
		final Data data = this.al.get(this.pos);
				
		AlertDialog.Builder ad = null;
		
		ad = new AlertDialog.Builder(getActivity());
		ad.setTitle("대표명함 지정");
		ad.setMessage("대표명함으로 지정하시겠습니까?");
		ad.setNegativeButton("확인", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				nid_check_2 = data.getNid();
				
				db = new DBHelper(getActivity(), "toyou", null, 1);
	
				boolean check = false;
				
				check = db.modifyMineTitle(data.getNid());
							
				if(check)
				{
					if(nid_check_1 == nid_check_2)
					{
						Toast.makeText(getActivity(), "이미 대표명함 입니다.", Toast.LENGTH_SHORT).show();
					}
					else if(nid_check_1 != nid_check_2)
					{
						Toast.makeText(getActivity(), "대표명함 지정 성공", Toast.LENGTH_SHORT).show();
						MainActivity.my_main_namecard = data;
						nid_check_1 = nid_check_2;
						nid_check_2 = 0;
						
						flag = true;
					}
					
					frag = null;
					frag = new MyFragment();
					FragmentTransaction trans = getFragmentManager().beginTransaction();
					trans.replace(R.id.root_layout, frag);
					trans.addToBackStack(null);
					trans.commitAllowingStateLoss();
				}
				else
				{
					Toast.makeText(getActivity(), "대표명함 지정 실패", Toast.LENGTH_SHORT).show();
				}
			}
			 
		});
	ad.setPositiveButton("취소", null);
	ad.show();
		return flag;
	}
	
	
	
	
	
	private class DataAdapter2 extends ArrayAdapter<Data> {
		private LayoutInflater mInflater;
		
		public DataAdapter2(Context context, ArrayList<Data> data) {
			super(context, 0, data);	
			
			mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
		}

		@Override
		public View getView(int position, View v, ViewGroup parent) {
			View view = null;
			if (v == null) {
				view = mInflater.inflate(R.layout.mylistview_item, null);
			} else {
				view = v;
			}
			final Data data = this.getItem(position);
			if (data != null) {
				nname = (TextView) view.findViewById(R.id.mylistview_layout_tv_nname);
				company= (TextView) view.findViewById(R.id.mylistview_layout_tv_company);
				grade = (TextView) view.findViewById(R.id.mylistview_layout_tv_grade);
				department = (TextView) view.findViewById(R.id.mylistview_layout_tv_department);
				
				address = (TextView) view.findViewById(R.id.mylistview_layout_tv_address);
				telNum = (TextView) view.findViewById(R.id.mylistview_layout_tv_telNum);
				phoneNum = (TextView) view.findViewById(R.id.mylistview_layout_tv_phoneNum);
				email= (TextView) view.findViewById(R.id.mylistview_layout_tv_email);
				
				root_myitem = (LinearLayout) view.findViewById(R.id.root);
				
				nname.setText(data.getNname());
				company.setText(data.getCompany());
				grade.setText(data.getGrade());
				department.setText(data.getDepartment());
				
				address.setText(data.getAddress());
				telNum.setText(data.getTelNum());
				phoneNum.setText(data.getPhoneNum());
				email.setText(data.getEmail());
				if(data.getMtitle()==1)
				{
					root_myitem.setBackgroundResource(R.drawable.card_sel);
					nid_check_1 = data.getNid();
				}else
				{
					root_myitem.setBackgroundResource(R.drawable.namecardbg);
				}
			}
			return view;
		}
		
	}



	
}
